package com.summer.framework.admin.config;

import com.baomidou.mybatisplus.extension.parsers.DynamicTableNameParser;
import com.baomidou.mybatisplus.extension.parsers.ITableNameHandler;
import com.baomidou.mybatisplus.extension.plugins.PaginationInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.Collections;
import java.util.HashMap;
import java.util.Random;
/**
 * @auther: 杨杰
 * @date: 2019/9/19 18:25
 * @description:
 */
@Configuration
@MapperScan("com.summer.framework.admin.mapper")
@Slf4j
public class MybatisPlus2Config {

    @Bean
    public PaginationInterceptor paginationInterceptor() {
        log.info("paginationInterceptor:");
        PaginationInterceptor paginationInterceptor = new PaginationInterceptor();
        DynamicTableNameParser dynamicTableNameParser = new DynamicTableNameParser();
        dynamicTableNameParser.setTableNameHandlerMap(new HashMap<String, ITableNameHandler>(2) {{
            put("users", (metaObject, sql, tableName) -> {
                // metaObject 可以获取传入参数，这里实现你自己的动态规则
                int random = new Random().nextInt(2);
                int result=(random % 3)+1;
                return tableName + "_"+result;
            });
        }});
        paginationInterceptor.setSqlParserList(Collections.singletonList(dynamicTableNameParser));
        return paginationInterceptor;
    }


}
